﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;

namespace Daq.Tools.GZip
{
    /// <summary>
    /// 数据转化帮助类
    /// </summary>
    public static class GZipHelper
    {
        #region 字符串转byte[]

        /// <summary>
        /// 字符串转化为byte[]
        /// </summary>
        /// <param name="value">字符串数据</param>
        /// <returns>转成功的byte[]</returns>
        public static byte[] ToBytesFromBase64(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return null;
            }
            FromBase64Transform transform = new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces);
            byte[] outputBuffer = new byte[transform.OutputBlockSize];
            byte[] bytes = Encoding.Default.GetBytes(value);
            MemoryStream stream = new MemoryStream(bytes.Length);
            using (stream)
            {
                int inputOffset = 0;
                int inputCount = 4;
                while ((bytes.Length - inputOffset) > inputCount)
                {
                    int count = transform.TransformBlock(bytes, inputOffset, inputCount, outputBuffer, 0);
                    inputOffset += inputCount;
                    if (count > 0)
                    {
                        stream.Write(outputBuffer, 0, count);
                    }
                }
                outputBuffer = transform.TransformFinalBlock(bytes, inputOffset, bytes.Length - inputOffset);
                stream.Write(outputBuffer, 0, outputBuffer.Length);
                transform.Clear();
                stream.Position = 0L;
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, (int)stream.Length);
                return buffer;
            }
        }

        #endregion 字符串转byte[]

        #region byte[]转字符串

        /// <summary>
        /// 字节数组转Base64字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToBase64String(byte[] data)
        {
            return Convert.ToBase64String(data, 0, data.Length);
        }

        #endregion byte[]转字符串

        #region byte[]压缩 至 byte[]

        /// <summary>
        /// byte[]压缩 至 byte[]
        /// </summary>
        /// <param name="buffer">字节流</param>
        /// <returns>压缩后的byte[]</returns>
        public static byte[] Compress(byte[] buffer)
        {
            if (buffer == null)
                return null;
            using (var ms = new MemoryStream())
            {
                using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(buffer, 0, buffer.Length);
                }
                return ms.ToArray();
            }
        }

        #endregion byte[]压缩 至 byte[]

        #region byte[]解压缩 至 byte[]

        /// <summary>
        /// byte[]解压缩
        /// </summary>
        /// <param name="buffer">数据字节</param>
        /// <returns>解压缩后的byte[]</returns>
        public static byte[] DecompressByte(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
            MemoryStream outBuffer = new MemoryStream();
            byte[] block = new byte[1024];

            while (true)
            {
                int bytesRead = compressedzipStream.Read(block, 0, block.Length);
                if (bytesRead <= 0)
                    break;
                else
                    outBuffer.Write(block, 0, bytesRead);
            }

            compressedzipStream.Close();
            return outBuffer.ToArray();
        }

        #endregion byte[]解压缩 至 byte[]
    }
}